home *** CD-ROM | disk | FTP | other *** search
- unit BasicThread;
-
- interface
-
- uses SysUtils,Classes,syncobjs,Windows,Messages,Forms;
-
- type
- TBasicThread=class(TThread)
- protected
- fException:Exception;
- procedure ShowException;
- function RemoveMessage(const RemoveMessage:UINT):boolean;
- procedure RemoveAllMessage;
- function PeekQuitMessage:boolean;
- public
- constructor Create(CreateSuspended: Boolean);
- destructor Destroy;override;
- end;
-
- const
- //WM_APP = Base for Application Message Numbers
- WM_Signal_RequestWordLength=WM_APP+0;
- WM_Signal_WordLengthIs=WM_APP+1;
- WM_Signal_RequestLetterUsedAtPosition=WM_APP+2;
- WM_Signal_Yes=WM_APP+3;
- WM_Signal_No=WM_App+4;
- WM_Signal_ThreadGotAnswer=WM_App+5;
- WM_Signal_ThreadTerminating=WM_App+6;
-
- var
- ThreadCounter:integer;
- //Note that synchronisation control is required on the 'ThreadCounter' if other
- //threads other than the main thread are responsible for creating threads.
-
- implementation
-
- constructor TBasicThread.Create(CreateSuspended:Boolean);
- begin
- //Were in the main VCL thread now since in this application only the main VCL
- //thread creates and destroys threads.
- inc(ThreadCounter);
- inherited Create(CreateSuspended);
- if Handle=0then
- raise exception.create('Unable to Create Thread');
- end;
-
- destructor TBasicThread.Destroy;
- begin
- inherited Destroy;
- Dec(ThreadCounter);
- end;
-
- procedure TBasicThread.ShowException;
- begin
- Application.ShowException(fException);
- end;
-
-
- function TBasicThread.RemoveMessage(const RemoveMessage:UINT):boolean;
- //True returned if message found and removed.
- var
- Msg:TMsg;
- begin
- Result:=FALSE;
- while(Windows.PeekMessage(Msg,0,RemoveMessage,RemoveMessage,PM_Remove))do
- Result:=TRUE;
- end;
-
- procedure TBasicThread.RemoveAllMessage;
- var
- Msg:TMsg;
- begin
- repeat
- until not(Windows.PeekMessage(Msg,0,0,0,PM_Remove));
- end;
-
- function TBasicThread.PeekQuitMessage:boolean;
- var
- Msg:TMsg;
- begin
- Result:=Windows.PeekMessage(Msg,0,WM_Quit,WM_Quit,PM_Remove);
- end;
-
- initialization
- ThreadCounter:=0;
-
- end.
-